//
//  TestSubmitAlertView.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/6/5.
//

#import "TestSubmitAlertView.h"

@interface TestSubmitAlertView()

@property(nonatomic,strong)UIView *bgView;
@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UIButton *cancelBtn;
@property(nonatomic,strong)UIButton *updateBtn;

@end

@implementation TestSubmitAlertView

-(instancetype)initWithFrame:(CGRect)frame andTitle:(NSString*)title andLeftTitle:(NSString*)leftTitle andleftColor:(UIColor*)leftColor andRightTitle:(NSString*)rightTitle andRightColor:(UIColor*)rightColor andTitleFont:(UIFont*)titleFont andLeftFont:(UIFont*)leftFont andRightFont:(UIFont*)rightFont{
    self = [super initWithFrame:frame];
    if(self){
        self.currentAvailableWidth = frame.size.width;
        self.currentAvailableHeight = frame.size.height;
        self.title = title;
        self.leftTitle = leftTitle;
        self.leftColor = leftColor;
        self.rightTitle = rightTitle;
        self.rightColor = rightColor;
        self.titleFont = titleFont;
        self.leftFont = leftFont;
        self.rightFont = rightFont;
        [self createUI];
    }
    return self;
}

-(void)createUI{
    
    self.backgroundColor = UIColorHex_(0x000000,0.4);
    __weak typeof(self)self_weak_ = self;
    CGFloat bg_w = 280;
    if(self.currentAvailableWidth < 312){
        bg_w = self.currentAvailableWidth-32;
    }
    CGFloat h = [self getStringHeightWithText:self.title font:self.titleFont viewWidth:bg_w-70];
    CGFloat top = 46;
    if(h < 22){
        h = 22;
    }
    if(h > 22&&h < 44){
        h =  44;
        top = 35;
    }else if ( h >= 44){
        top = 35;
    }
    CGFloat total_h = top*2+h+48;
    UIView *bgView = [[UIView alloc]init];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.cornerRadius = 10;
    bgView.layer.masksToBounds = YES;
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(bg_w);
        make.centerX.mas_equalTo(self_weak_);
        make.height.mas_equalTo(total_h);
        make.centerY.mas_equalTo(self_weak_);
    }];
    self.bgView = bgView;
    
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.font = self.titleFont;
    titleLabel.numberOfLines = 0;
    titleLabel.textColor = KFontBlackColor;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = self.title;
    [bgView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(bg_w-70);
        make.centerX.mas_equalTo(bgView);
        make.height.mas_equalTo(h);
        make.centerY.mas_equalTo(bgView).offset(-24);
    }];
    self.titleLabel = titleLabel;
    
    CGFloat btn_w = (bg_w-2)/2.0;
    UIButton *cancelBtn = [[UIButton alloc]init];
    [cancelBtn setTitle:self.rightTitle forState:UIControlStateNormal];
    [cancelBtn setTitleColor:self.rightColor forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = self.rightFont;
    [cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:cancelBtn];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.mas_equalTo(@0);
        make.height.mas_equalTo(@48);
        make.width.mas_equalTo(btn_w);
    }];
    self.cancelBtn = cancelBtn;
    
    UIButton *updateBtn = [[UIButton alloc]init];
    [updateBtn setTitle:self.leftTitle forState:UIControlStateNormal];
    [updateBtn setTitleColor:self.leftColor forState:UIControlStateNormal];
    updateBtn.titleLabel.font = self.leftFont;
    [updateBtn addTarget:self action:@selector(updateBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:updateBtn];
    [updateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.mas_equalTo(@0);
        make.height.mas_equalTo(@48);
        make.width.mas_equalTo(btn_w);
    }];
    self.updateBtn = updateBtn;
    
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = KLineColor;
    [bgView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(@-48.5);
        make.height.mas_equalTo(0.5);
        make.left.right.mas_equalTo(@0);
    }];
    
    UIView *line2 = [[UIView alloc]init];
    line2.backgroundColor = KLineColor;
    [bgView addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(@0);
        make.width.mas_equalTo(0.5);
        make.height.mas_equalTo(@48);
        make.centerX.mas_equalTo(bgView);
    }];
    
}

-(void)refreshWidth{
    CGFloat bg_w = 280;
    if(self.currentAvailableWidth < 312){
        bg_w = self.currentAvailableWidth-32;
    }
    CGFloat h = [self getStringHeightWithText:self.title font:self.titleFont viewWidth:bg_w-70];
    CGFloat top = 46;
    if(h < 22){
        h = 22;
    }
    if(h > 22&&h < 44){
        h =  44;
        top = 35;
    }else if ( h >= 44){
        top = 35;
    }
    CGFloat total_h = top*2+h+48;
    
    [self.bgView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(bg_w);
        make.height.mas_equalTo(total_h);
    }];
    [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(bg_w-70);
        make.height.mas_equalTo(h);
    }];
    CGFloat btn_w = (bg_w-2)/2.0;
    [self.cancelBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(btn_w);
    }];
    [self.updateBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(btn_w);
    }];
}

-(void)cancelBtnClick{
    if(_rightBlock){
        _rightBlock();
    }
}

-(void)updateBtnClick{
    if(_leftBlock){
        _leftBlock();
    }
}



@end
